package com.ebei.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ebei.admin.common.util.AuthRealm;
import com.ebei.admin.model.dto.RoleTree;
import com.ebei.admin.model.entity.SysRole;
import com.ebei.admin.service.SysRoleMenuService;
import com.ebei.admin.service.SysRoleService;
import com.ebei.common.constant.CommonConstant;
import com.ebei.common.util.Query;
import com.ebei.common.util.Result;
import com.ebei.common.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Api(value = "角色", description = "角色接口")
public class RoleController extends BaseController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @ApiOperation(value = "添加角色",notes = "添加角色")
    @ApiImplicitParam(name = "sysRole", value = "角色实体", required = true)
    @PostMapping("/addRole")
    public Result<Boolean> addRole(@RequestBody SysRole sysRole){
        return sysRoleService.addRole(sysRole);
    }

    @ApiOperation(value = "修改角色",notes = "修改角色")
    @ApiImplicitParam(name = "sysRole", value = "角色实体", required = true)
    @PostMapping("/updateRole")
    public Result<Boolean> updateRole(@RequestBody SysRole sysRole){

        return sysRoleService.updateRole(sysRole);


    }

    @ApiOperation(value = "删除角色",notes = "删除角色")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true)
    @PostMapping("/roleDel")
    public Result<Boolean> roleDel(@RequestBody Map<String,Object> params) {
        Result<Boolean> result = new Result<>();
        if(sysRoleService.deleteByRoleId(params.get("id").toString())){
            result.setErrorMessage("删除成功");
            result.setErrorCode(Result.SUCCESS);
        }else{
            result.setErrorMessage("删除失败");
            result.setErrorCode(Result.FAIL);
        }
        return result;
    }

    /**
     * 获取角色列表
     *
     * @param deptId  部门ID
     * @return 角色列表
     */
    @GetMapping("/roleList/{deptId}")
    public List<SysRole> roleList(@PathVariable Integer deptId) {
        return sysRoleService.selectListByDeptId(deptId);

    }

    /**
     * 分页查询角色信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/rolePage")
    public Page rolePage(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return sysRoleService.selectwithDeptPage(new Query<>(params), new EntityWrapper<>());
    }

    @ApiOperation("角色关联菜单")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "roleCode", value = "角色code", required = true),
        @ApiImplicitParam(name = "json", value = "菜单json", required = true)
    })
    @PostMapping("/roleMenuUpd")
    public Result<Boolean> roleMenuUpd(@RequestBody JSONObject params){

        if(this.sysRoleMenuService.roleMenuUpd(params)){
            return new Result<>(Boolean.TRUE,Result.SUCCESS,"更新成功");
        }
        else{
            return new Result<>(Boolean.FALSE,Result.FAIL,"更新失败");
        }


    }

    @ApiOperation(value = "获取角色树",notes = "角色模板页面初始化")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @GetMapping("/getTree")
    public List<RoleTree> getTree(){
        return this.sysRoleService.getTree(AuthRealm.getPrincipal().getUserId());
    }

    @ApiOperation(value = "更新角色用户",notes = "更新指定角色所关联用户")
    @GetMapping("/roleUserUpdate")
    public Result<Boolean> roleUserUpdate(){
        return null;
    }

    @ApiOperation(value = "获取角色集合",notes = "获取角色集合")
    @GetMapping("/getAllRoles")
    public Result<List<SysRole>> getAllRoles(){
        return this.sysRoleService.getAllRoles();
    }

}
